using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Data.SqlClient;
using System.Data.Common;

public partial class paging : System.Web.UI.Page
{
    const int noRecordPerPage = 10; //so record tren 1 page
    const int noPagePerSegment = 16; //bao nhieu trang tren mot phan doan
    const string strPage = "p";


    protected void Page_Load(object sender, EventArgs e)
    {
        int page = 1;
        if (Request.QueryString[strPage] != null)
        {
            page = int.Parse(Request.QueryString[strPage]);
        }

        getData(page);
        createPaging(page);
    }

    private void getData(int Page)
    {
        DataTable dt = PagingData(Page, noRecordPerPage);
        if (dt.Rows.Count > 0)
        {
            ViewState["Total"] = dt.Rows[0]["TotalRows"].ToString();
            rep1.DataSource = dt;
            rep1.DataBind();
        }
    }

    private DataTable PagingData(int Page, int PageSize)
    {
        //parms        
        SqlParameter[] parms = new SqlParameter[]
                    {                        
                        new SqlParameter("@Page",Page),
                        new SqlParameter("@PageSize",PageSize)
                    };      
        DataTable dt=SqlHelper.ExecuteDataset(CommandType.StoredProcedure,"Paging",parms).Tables[0];
        return dt;
    }

    

    private void createPaging(int page)
    {
        Literal ltrDisplay = new Literal();
        //ltrDisplay.Text = string.Format("<br>Total <b>{0}</b> records",rowCount);
        Pager p = new Pager(page, noRecordPerPage, noPagePerSegment,int.Parse(ViewState["Total"].ToString()));
        p.Next = "<img style=\"vertical-align:middle\" src=\"Images/next.png\" border=\"0\"/>";
        p.Previous = "<img style=\"vertical-align:middle\" src=\"Images/prev.png\" border=\"0\"/>";

        ltrDisplay.Text =p.CreateNavigation();
        plcPaging.Controls.Add(ltrDisplay);
    }

    
}